﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration.ConfigurationManager

Partial Class CSearch
    Inherits System.Web.UI.Page

    Dim Equipo As New Equipo
    'instanciar la conexion
    Dim cn As New SqlConnection(ConnectionStrings("cn").ConnectionString)



    Protected Sub btnConsulta_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnConsulta.Click

        Dim StrQuery As String = GenerarQuery()

        Dim dt As DataTable = Equipo.ListadoParametro(StrQuery)
        GridView1.DataSource = dt
        GridView1.DataBind()
        GridView2.DataSource = dt
        GridView2.DataBind()
        Session.Add("dt", dt)
        '   BuildGrid(dt, tblGrid)
    End Sub

    Function GenerarQuery() As String
        Dim StrQuery As String = ""
        Dim StrQuery1 As String = " select t.RNumber ""top"", t.Descripcion ""Descripcion"" ,t.Cantidad Cantidad from " & _
                                    "( select ROW_NUMBER() over (order by SUM(de.cantidad) "
        Dim StrQuery2 As String = " ) as RNumber, " & _
                                  " eq.Equip_desc  As ""Descripcion"" , " & _
                                  " SUM(de.cantidad) Cantidad   " & _
                                  " from dbo.tb_detalle_orden_compra as de " & _
                                    " inner join tb_equipo as eq " & _
                                    " on  de.cod_equipo = eq.cod_equipo " & _
                                    " group by  eq.Equip_desc  ) as t " & _
                                " where t.RNumber between 1 and "


        If cboTipo.SelectedValue.Trim = "1" Then

            StrQuery2 = StrQuery2 & " 5 "

        ElseIf cboTipo.SelectedValue.Trim = "2" Then
            StrQuery2 = StrQuery2 & " 10 "

        ElseIf cboTipo.SelectedValue.Trim = "3" Then
            StrQuery2 = StrQuery2 & " 100 "

        End If

        If RdFiltro.SelectedValue.Trim = "01" Then
            StrQuery1 = StrQuery1 & "Asc "

        Else
            StrQuery1 = StrQuery1 & "Desc "
        End If

        StrQuery = StrQuery1 & StrQuery2

        Return StrQuery

    End Function


    Protected Sub GridView2_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView2.PageIndexChanging
        'Cambio de Página
        GridView2.PageIndex = e.NewPageIndex
        'actualizar
        GridView2.DataSource = CType(Session.Item("dt"), DataTable)
        GridView2.DataBind()
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Master.MostrarClienteSession()
    End Sub
End Class
